package models

import (
	"github.com/jinzhu/gorm"
)

type Category struct {
	Model
	Pid    int        `json:"pid"`
	Name   string     `json:"name"`
	Weigh  int        `json:"weigh"`
	Flag   int        `json:"flag"`
	Scenes []Category `gorm:"foreignKey:Pid;references:ID"` //自关联,pid作为外键

}

func GetTagsWithScenes() ([]Category, error) {
	var types []Category
	// 使用链式调用并检查错误
	result := db.Where("flag = ?", 1).Order("weigh").Where("status = ?", "normal").
		Preload("Scenes", func(db *gorm.DB) *gorm.DB {
			return db.Where("flag = ?", 2)
		}).
		Find(&types)

	// 返回数据和错误
	return types, result.Error
}
